﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using NPOI.POIFS.Crypt.Dsig;
using NPOI.XWPF.UserModel;
using Org.BouncyCastle.Bcpg;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK_WMS.Repository
{
    public class PDAEmptyTrayRepository : BaseRepository<TPallet>, IPDAEmptyTrayRepository
    {
        public PDAEmptyTrayRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }



        /// <summary>
        /// 查看数据是否满足条件
        /// </summary>
        /// <param name="PalletBarcode"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool CreateStackingStorage(string PalletBarcode, out string msg)
        {
            msg = string.Empty;
            List<TStation> inAndOutPortStations = Context.TStations
                .Where(it => it.StationType == 1).ToList();
            if (inAndOutPortStations == null || inAndOutPortStations.Count == 0)
            {
                msg = "未找到可用的出入口站台";
                return false;
            }
            int storyCount = Context.TInventory.Where(it => it.PalletBarcode == PalletBarcode).Count();
            if (storyCount != 0)
            {
                msg = "当前托盘已在库存中";
                return false;
            }

            //int stackCount = Context.TStackingStorages.Where(it => it.PalletBarcode == PalletBarcode).Count();
            //if (stackCount > 1)
            //{
            //    msg = "请不要重复绑定相同垛";
            //    return false;
            //}



            return true;
        }

        /// <summary>
        /// 添加入库单
        /// </summary>
        /// <param name="PalletBarcode"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool AddEmptyTrayReceivingOrder(string PalletBarcode, string WarehouseCode, out string msg)
        {
            msg = string.Empty;
            List<TStation> inAndOutPortStations = Context.TStations
                .Where(it => it.StationType == 1).ToList();
            if (inAndOutPortStations == null || inAndOutPortStations.Count == 0)
            {
                msg = "未找到可用的出入口站台";
                return false;
            }
            int storyCount = Context.TInventory.Where(it => it.PalletBarcode == PalletBarcode).Count();
            if (storyCount != 0)
            {
                msg = "当前托盘已在库存中";
                return false;
            }
            var warehouseList = Context.TWarehouses.Where(x => x.Code == WarehouseCode).ToList();
            if (warehouseList.Count == 0)
            {
                msg = "暂无此库房";
                return false;
            }
            if (warehouseList.First().Status == 0)
            {
                msg = "库房已被禁用";
                return false;
            }
            TReceivingOrderDetail receivingOrderDetail = new TReceivingOrderDetail();
            DateTime now = DateTime.Now;
            string year = now.Year.ToString();
            string month = now.Month.ToString().PadLeft(2, '0');
            string day = now.Day.ToString().PadLeft(2, '0');
            string hour = now.Hour.ToString().PadLeft(2, '0');
            string minute = now.Minute.ToString().PadLeft(2, '0');
            string second = now.Second.ToString().PadLeft(2, '0');
            receivingOrderDetail.OrderSn = "R" + $"{year}{month}{day}{hour}{minute}{second}";
            receivingOrderDetail.BillType = 1;
            receivingOrderDetail.WarehouseCode = WarehouseCode;
            receivingOrderDetail.PalletBarcode = PalletBarcode;
            receivingOrderDetail.Qty = 1;
            receivingOrderDetail.ReceivedQty = 1;
            receivingOrderDetail.CreateDate = DateTime.Now;
            if (GetWarehoseName(WarehouseCode) == null)
            {
                msg = "未找到该库房";
                return false;
            }
            receivingOrderDetail.WarehouseName = GetWarehoseName(WarehouseCode);
            receivingOrderDetail.MaterialName = "空托盘";
            receivingOrderDetail.Status = 2;
            Context.TReceivingOrderDetails.Add(receivingOrderDetail);
            if (Context.SaveChanges() > 0)
            {
                return true;
            }
            msg = "添加入库单失败";
            return false;

        }


        /// <summary>
        /// 修改其他状态
        /// </summary>
        /// <param name="PalletBarcode"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool UpdEmptyTrayStatus(string PalletBarcode, string WarehouseCode, out string msg)
        {
            using (var tran = Context.Database.BeginTransaction())
                try
                {
                    msg = string.Empty;
                    List<TStation> inAndOutPortStations = Context.TStations
                        .Where(it => it.StationType == 1).ToList();
                    if (inAndOutPortStations == null || inAndOutPortStations.Count == 0)
                    {
                        msg = "未找到可用的出入口站台";
                        return false;
                    }
                    var palletListStatus = Context.TPallets.Where(x => x.PalletBarcode == PalletBarcode && x.Status == 0).ToList();
                    if(palletListStatus.Count == 0)
                    {
                        msg = "该托盘已被禁用";
                        return false;
                    }
                    int storyCount = Context.TInventory.Where(it => it.PalletBarcode == PalletBarcode).Count();
                    if (storyCount != 0)
                    {
                        msg = "当前托盘已在库存中";
                        return false;
                    }
                    var warehouseList = Context.TWarehouses.Where(x => x.Code == WarehouseCode).ToList();
                    if (warehouseList.Count == 0)
                    {
                        msg = "暂无此库房";
                        return false;
                    }
                    if (warehouseList.First().Status == 0)
                    {
                        msg = "库房已被禁用";
                        return false;
                    }
                    TReceivingOrderDetail receivingOrderDetail = new TReceivingOrderDetail();
                    DateTime now = DateTime.Now;
                    string year = now.Year.ToString();
                    string month = now.Month.ToString().PadLeft(2, '0');
                    string day = now.Day.ToString().PadLeft(2, '0');
                    string hour = now.Hour.ToString().PadLeft(2, '0');
                    string minute = now.Minute.ToString().PadLeft(2, '0');
                    string second = now.Second.ToString().PadLeft(2, '0');
                    receivingOrderDetail.OrderSn = "R" + $"{year}{month}{day}{hour}{minute}{second}";
                    receivingOrderDetail.BillType = 5;
                    receivingOrderDetail.WarehouseCode = WarehouseCode;
                    receivingOrderDetail.PalletBarcode = PalletBarcode;
                    receivingOrderDetail.Qty = 1;
                    receivingOrderDetail.ReceivedQty = 1;
                    receivingOrderDetail.CreateDate = DateTime.Now;
                    if (GetWarehoseName(WarehouseCode) == null)
                    {
                        msg = "未找到该库房";
                        tran.Rollback();
                        return false;
                    }
                    receivingOrderDetail.WarehouseName = GetWarehoseName(WarehouseCode);
                    receivingOrderDetail.MaterialName = "空托盘";
                    receivingOrderDetail.Status = 2;
                    Context.TReceivingOrderDetails.Add(receivingOrderDetail);
                    if (Context.SaveChanges() <= 0)
                    {
                        msg = "添加入库单失败";
                        tran.Rollback();
                        return false;
                    }


                    msg = "";
                    // 查询是否有可用仓位
                    var cellList = Context.TCells.Where(x => x.Status == 1 && x.FullStatus == 0).ToList();
                    if (cellList.Count <= 0)
                    {
                        msg = "暂无可用仓位";
                        tran.Rollback();
                        return false;
                    }
                    string cellCode = cellList[0].Code;
                    //修改托盘表
                    var palletList = Context.TPallets.Where(x => x.PalletBarcode == PalletBarcode);
                    if (palletList.Count() <= 0)
                    {
                        msg = "未查询到此托盘";
                        tran.Rollback();
                        return false;
                    }
                    var pallbarList = palletList.First();
                    //var warehouseList = Context.TWarehouses.Where(x => x.Code == WarehouseCode).ToList();
                    pallbarList.LocationCode = cellCode;
                    pallbarList.LocationName = cellList[0].Name;
                    pallbarList.WarehouseCode = WarehouseCode;
                    pallbarList.WarehouseName = warehouseList != null ? warehouseList[0].Name : null;
                    pallbarList.WarehouseId = warehouseList != null ? warehouseList[0].Id : 0;
                    Context.TPallets.Update(pallbarList);
                    if (Context.SaveChanges() <= 0)
                    {
                        msg = "修改托盘表数据失败";
                        tran.Rollback();
                        return false;
                    }
                    //添加入库垛信息
                    TStackingStorage stackingStorage = new TStackingStorage();
                    stackingStorage.WarehouseCode = pallbarList.WarehouseCode;
                    stackingStorage.WarehouseName = pallbarList.WarehouseName;
                    stackingStorage.BillType = 1;
                    stackingStorage.Status = 1;
                    stackingStorage.PalletBarcode = pallbarList.PalletBarcode;
                    stackingStorage.PackQty = 1;
                    stackingStorage.Qty = 1;
                    stackingStorage.ExpectedLocation = pallbarList.LocationCode;
                    stackingStorage.ActualLocation = pallbarList.LocationCode;
                    stackingStorage.CreateDate = DateTime.Now;
                    stackingStorage.Fifodate = DateTime.Now;
                    stackingStorage.MaterialName = "空托盘";
                    Context.TStackingStorages.Add(stackingStorage);
                    if (Context.SaveChanges() <= 0)
                    {
                        msg = "添加入库垛数据失败";
                        tran.Rollback();
                        return false;
                    }
                    //库存表新增数据
                    var stackingList = GetStacking(PalletBarcode);
                    TInventory inventory = new TInventory();
                    inventory.WarehouseId = pallbarList.WarehouseId;
                    inventory.WarehouseCode = pallbarList.WarehouseCode;
                    inventory.WarehouseName = pallbarList.WarehouseName;
                    inventory.LocationCode = pallbarList.LocationCode;
                    inventory.LocationName = pallbarList.LocationName;
                    inventory.LocationId = cellList.Where(x => x.Code == pallbarList.LocationCode).First().Id;
                    inventory.Status = 1;
                    inventory.PalletBarcode = PalletBarcode;
                    inventory.BillType = 1;
                    inventory.CreateDate = DateTime.Now;
                    inventory.MaterialName = "空托盘";
                    inventory.StackingStorageId = stackingList.Id;
                    Context.TInventories.Add(inventory);
                    if (Context.SaveChanges() <= 0)
                    {
                        msg = "添加库存数据失败";
                        tran.Rollback();
                        return false;
                    }
                    //库存流水添加数据
                    DateTime nowDouble = DateTime.Now;
                    List<TReceivingOrderDetail> dataList = Context.TReceivingOrderDetails.Where(x => x.PalletBarcode == PalletBarcode).ToList(); // 获取数据列表
                    var query = from data in dataList
                                orderby Math.Abs((data.CreateDate - nowDouble).Ticks) ascending
                                select data;
                    if (query.Count() <= 0)
                    {
                        msg = "入库单暂无数据";
                        tran.Rollback();
                        return false;
                    }
                    TInventoryLog inventoryLog = new TInventoryLog();
                    inventoryLog.SourceType = 0;
                    inventoryLog.SourceSn = query.First().OrderSn;
                    inventoryLog.WarehouseId = pallbarList.WarehouseId;
                    inventoryLog.WarehouseName = pallbarList.WarehouseName;
                    inventoryLog.LocationCode = pallbarList.LocationCode;
                    inventoryLog.PalletBarcode = PalletBarcode;
                    inventoryLog.MaterialName = "空托盘";
                    inventoryLog.CreateDate = DateTime.Now;
                    Context.TInventoryLog.Add(inventoryLog);
                    if (Context.SaveChanges() <= 0)
                    {
                        msg = "添加库存数据失败";
                        tran.Rollback();
                        return false;
                    }
                    //修改仓位表状态
                    var locationList = cellList.Where(x => x.Code == pallbarList.LocationCode).ToList();
                    if (locationList.Count() <= 0)
                    {
                        msg = "修改仓位表失败";
                        tran.Rollback();
                        return false;
                    }
                    locationList[0].FullStatus = 2;
                    //入库单状态修改
                    TReceivingOrderDetail receivingOrderDetailDouble = dataList.FirstOrDefault(x => x.PalletBarcode == PalletBarcode);
                    receivingOrderDetailDouble.Status = 3;
                    Context.TReceivingOrderDetails.Update(receivingOrderDetailDouble);
                    if (Context.SaveChanges() <= 0)
                    {
                        msg = "添加入库单数据失败";
                        tran.Rollback();
                        return false;
                    }

                    tran.Commit();
                    return true;
                }
                catch
                {
                    msg = "发生错误!";
                    tran.Rollback();
                    return false;
                }

        }

        /// <summary>
        /// 查看数据是否满足条件(出库)
        /// </summary>
        /// <param name="PalletBarcode"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool CreateStackingStorageOut(string PalletBarcode, out string msg)
        {
            msg = string.Empty;
            List<TStation> inAndOutPortStations = Context.TStations
                .Where(it => it.StationType == 1).ToList();
            if (inAndOutPortStations == null || inAndOutPortStations.Count == 0)
            {
                msg = "未找到可用的出入口站台";
                return false;
            }
            int storyCount = Context.TInventory.Where(it => it.PalletBarcode == PalletBarcode).Count();
            if (storyCount == 0)
            {
                msg = "当前托盘不在库存中";
                return false;
            }
            //int stackCount = Context.TStackingStorages.Where(it => it.PalletBarcode == PalletBarcode).Count();
            //if (stackCount > 1)
            //{
            //    msg = "请不要重复绑定相同垛";
            //    return false;
            //}


            return true;
        }


        /// <summary>
        /// 添加出库单
        /// </summary>
        /// <param name="PalletBarcode"></param>
        /// <param name="WarehouseCode"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public string AddEmptyTrayShippingOrder(string PalletBarcode, string WarehouseCode, out string msg)
        {

            msg = string.Empty;
            var warehouseList = Context.TWarehouses.Where(x => x.Code == WarehouseCode).ToList();
            if (warehouseList.Count == 0)
            {
                msg = "暂无此库房";
                return null;
            }
            if (warehouseList.First().Status == 0)
            {
                msg = "库房已被禁用";
                return null;
            }
            TShippingOrderDetail detail = new TShippingOrderDetail();
            TShippingLockCell shippingLockCell = new TShippingLockCell();
            DateTime now = DateTime.Now;
            string year = now.Year.ToString();
            string month = now.Month.ToString().PadLeft(2, '0');
            string day = now.Day.ToString().PadLeft(2, '0');
            string hour = now.Hour.ToString().PadLeft(2, '0');
            string minute = now.Minute.ToString().PadLeft(2, '0');
            string second = now.Second.ToString().PadLeft(2, '0');
            detail.OrderSn = "C" + $"{year}{month}{day}{hour}{minute}{second}";
            detail.BillType = 1;
            detail.WarehouseCode = WarehouseCode;
            detail.PalletBarcode = PalletBarcode;
            detail.Qty = 1;
            detail.ShippedQty = 1;
            detail.CreateDate = DateTime.Now;
            if (GetWarehoseName(WarehouseCode) == null)
            {
                msg = "未找到该库房";
                return null;
            }
            detail.WarehouseName = GetWarehoseName(WarehouseCode);
            detail.MaterialName = "空托盘";
            detail.Status = 2;
            Context.TShippingOrderDetails.Add(detail);
            if (Context.SaveChanges() <= 0)
            {
                msg = "添加出库单失败";
                return null;
            }
            shippingLockCell.OrderDetailId = detail.Id;
            shippingLockCell.OrdeSn = detail.OrderSn;
            var InventoryList = GetInventoryId(PalletBarcode, WarehouseCode);
            if (InventoryList == null)
            {
                msg = "未查询到此库存";
                return null;
            }
            shippingLockCell.InventoryId = InventoryList.Id;
            shippingLockCell.CellId = InventoryList.LocationId;
            shippingLockCell.CellCode = InventoryList.LocationCode;
            shippingLockCell.CellName = InventoryList.LocationName;
            shippingLockCell.PalletBarcode = PalletBarcode;
            //入库垛信息
            shippingLockCell.StackingStorageId = InventoryList.StackingStorageId;
            shippingLockCell.OutQty = 1;
            shippingLockCell.InventoryQty = InventoryList.AvailableQty;
            shippingLockCell.MaterialName = "空托盘";
            shippingLockCell.CreateDate = DateTime.Now;
            Context.TShippingLockCells.Add(shippingLockCell);
            if (Context.SaveChanges() <= 0)
            {
                msg = "添加出库单明细失败";
                return null;
            }
            return detail.OrderSn;

        }

        /// <summary>
        /// 修改其他状态(出库)
        /// </summary>
        /// <param name="OrderSn"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool UpdEmptyTrayStatusOut(string PalletBarcode, string WarehouseCode, out string msg)
        {
            using (var tran = Context.Database.BeginTransaction())
                try
                {
                    msg = "";

                    msg = string.Empty;
                    List<TStation> inAndOutPortStations = Context.TStations
                        .Where(it => it.StationType == 1).ToList();
                    if (inAndOutPortStations == null || inAndOutPortStations.Count == 0)
                    {
                        msg = "未找到可用的出入口站台";
                        return false;
                    }
                    var palletListStatus = Context.TPallets.Where(x => x.PalletBarcode == PalletBarcode && x.Status == 0).ToList();
                    if (palletListStatus.Count == 0)
                    {
                        msg = "该托盘已被禁用";
                        return false;
                    }
                    int storyCount = Context.TInventory.Where(it => it.PalletBarcode == PalletBarcode).Count();
                    if (storyCount == 0)
                    {
                        msg = "当前托盘不在库存中";
                        return false;
                    }
                    //msg = string.Empty;
                    var warehouseList = Context.TWarehouses.Where(x => x.Code == WarehouseCode).ToList();
                    if (warehouseList.Count == 0)
                    {
                        msg = "暂无此库房";
                        return false;
                    }
                    if (warehouseList.First().Status == 0)
                    {
                        msg = "库房已被禁用";
                        return false;
                    }
                    TShippingOrderDetail detail = new TShippingOrderDetail();
                    TShippingLockCell shippingLockCell = new TShippingLockCell();
                    DateTime now = DateTime.Now;
                    string year = now.Year.ToString();
                    string month = now.Month.ToString().PadLeft(2, '0');
                    string day = now.Day.ToString().PadLeft(2, '0');
                    string hour = now.Hour.ToString().PadLeft(2, '0');
                    string minute = now.Minute.ToString().PadLeft(2, '0');
                    string second = now.Second.ToString().PadLeft(2, '0');
                    detail.OrderSn = "C" + $"{year}{month}{day}{hour}{minute}{second}";
                    detail.BillType = 1;
                    detail.WarehouseCode = WarehouseCode;
                    detail.PalletBarcode = PalletBarcode;
                    detail.Qty = 1;
                    detail.ShippedQty = 1;
                    detail.CreateDate = DateTime.Now;
                    if (GetWarehoseName(WarehouseCode) == null)
                    {
                        msg = "未找到该库房";
                        tran.Rollback();
                        return false;
                    }
                    detail.WarehouseName = GetWarehoseName(WarehouseCode);
                    detail.MaterialName = "空托盘";
                    detail.Status = 2;
                    Context.TShippingOrderDetails.Add(detail);
                    if (Context.SaveChanges() <= 0)
                    {
                        msg = "添加出库单失败";
                        tran.Rollback();
                        return false;
                    }
                    shippingLockCell.OrderDetailId = detail.Id;
                    shippingLockCell.OrdeSn = detail.OrderSn;
                    var InventoryList = GetInventoryId(PalletBarcode, WarehouseCode);
                    if (InventoryList == null)
                    {
                        msg = "未查询到此库存";
                        tran.Rollback();
                        return false;
                    }
                    shippingLockCell.InventoryId = InventoryList.Id;
                    shippingLockCell.CellId = InventoryList.LocationId;
                    shippingLockCell.CellCode = InventoryList.LocationCode;
                    shippingLockCell.CellName = InventoryList.LocationName;
                    shippingLockCell.PalletBarcode = PalletBarcode;
                    //入库垛信息
                    shippingLockCell.StackingStorageId = InventoryList.StackingStorageId;
                    shippingLockCell.OutQty = 1;
                    shippingLockCell.InventoryQty = InventoryList.AvailableQty;
                    shippingLockCell.MaterialName = "空托盘";
                    shippingLockCell.CreateDate = DateTime.Now;
                    Context.TShippingLockCells.Add(shippingLockCell);
                    if (Context.SaveChanges() <= 0)
                    {
                        msg = "添加出库单明细失败";
                        tran.Rollback();
                        return false;
                    }

                    //查询出库单表和出库单详情表
                    var shippingList = GetShippingOrderDetail(detail.OrderSn);
                    var shippingLockCellDouble = GetShippingLockCell(detail.OrderSn);

                    //查询仓位是否被使用
                    var cellList = Context.TInventories.Where(x => x.LocationCode == shippingLockCellDouble.CellCode);
                    if (cellList.Count() <= 0)
                    {
                        msg = "此仓位内无物料";
                        tran.Rollback();
                        return false;
                    }

                    //查询托盘是否在库存中
                    var palletBarList = Context.TInventories.Where(x => x.PalletBarcode == shippingLockCellDouble.PalletBarcode);
                    if (palletBarList.Count() <= 0)
                    {
                        msg = "托盘未在库存中";
                        tran.Rollback();
                        return false;
                    }

                    //修改托盘表
                    var palletList = Context.TPallets.Where(x => x.PalletBarcode == shippingLockCellDouble.PalletBarcode);
                    if (palletList.Count() <= 0)
                    {
                        msg = "暂无此托盘";
                        tran.Rollback();
                        return false;
                    }
                    var pallet = palletList.First();
                    pallet.LocationCode = null;
                    pallet.LocationName = null;
                    pallet.WarehouseId = null;
                    pallet.WarehouseCode = null;
                    pallet.WarehouseName = null;
                    Context.TPallets.Update(pallet);
                    if (Context.SaveChanges() <= 0)
                    {
                        msg = "修改托盘表失败";
                        tran.Rollback();
                        return false;
                    }

                    //修改入库垛信息
                    var stackingStoragesList = Context.TStackingStorages
                        .Where(x => x.PalletBarcode == shippingLockCellDouble.PalletBarcode && x.ActualLocation == shippingLockCellDouble.CellCode);
                    if (stackingStoragesList.Count() <= 0)
                    {
                        msg = "入库垛信息查询失败";
                        tran.Rollback();
                        return false;
                    }
                    var stackingList = stackingStoragesList.First();
                    stackingList.Status = 0;
                    Context.TStackingStorages.Update(stackingList);
                    if (Context.SaveChanges() <= 0)
                    {
                        msg = "修改入库垛表失败";
                        tran.Rollback();
                        return false;
                    }

                    //库存表删除数据
                    var inventoryList = Context.TInventories
                        .Where(x => x.PalletBarcode == shippingLockCellDouble.PalletBarcode && x.LocationCode == shippingLockCellDouble.CellCode);
                    if (inventoryList.Count() <= 0)
                    {
                        msg = "库存信息表查询失败";
                        tran.Rollback();
                        return false;
                    }
                    var inventoriesList = inventoryList.First();
                    Context.TInventories.Remove(inventoriesList);
                    if (Context.SaveChanges() <= 0)
                    {
                        msg = "删除库存表数据失败";
                        tran.Rollback();
                        return false;
                    }

                    //库存流水增加数据
                    DateTime nowDouble = DateTime.Now;
                    List<TShippingLockCell> dataList = Context.TShippingLockCells.Where(x => x.PalletBarcode == shippingLockCellDouble.PalletBarcode).ToList();

                    var sortedDataList = dataList.OrderBy(data => Math.Abs((data.CreateDate - nowDouble)?.Ticks ?? 0)).ToList();
                    if (sortedDataList.Count() <= 0)
                    {
                        msg = "入库单暂无数据";
                        tran.Rollback();
                        return false;
                    }
                    TInventoryLog inventoryLog = new TInventoryLog();
                    inventoryLog.SourceType = 1;
                    inventoryLog.SourceSn = sortedDataList.First().OrdeSn;
                    inventoryLog.WarehouseId = palletList.First().WarehouseId;
                    inventoryLog.WarehouseName = palletList.First().WarehouseName;
                    inventoryLog.LocationCode = palletList.First().LocationCode;
                    inventoryLog.PalletBarcode = shippingLockCellDouble.PalletBarcode;
                    inventoryLog.MaterialName = "空托盘";
                    inventoryLog.CreateDate = DateTime.Now;
                    Context.TInventoryLog.Add(inventoryLog);
                    if (Context.SaveChanges() <= 0)
                    {
                        msg = "添加库存数据失败";
                        tran.Rollback();
                        return false;
                    }

                    //修改仓位表
                    var locationList = Context.TCells.Where(x => x.Code == shippingLockCellDouble.CellCode).ToList();
                    if (locationList.Count() <= 0)
                    {
                        msg = "修改仓位表失败";
                        tran.Rollback();
                        return false;
                    }
                    locationList[0].FullStatus = 0;

                    //修改出库单状态
                    var shippingOrderDetail = Context.TShippingOrderDetails.Where(x => x.OrderSn == sortedDataList.First().OrdeSn);
                    if (shippingOrderDetail.Count() <= 0)
                    {
                        msg = "暂无此出库单";
                        tran.Rollback();
                        return false;
                    }
                    var shippingOrderList = shippingOrderDetail.First();
                    shippingOrderList.Status = 4;
                    Context.TShippingOrderDetails.Update(shippingOrderList);
                    if (Context.SaveChanges() <= 0)
                    {
                        msg = "修改出库单状态失败";
                        tran.Rollback();
                        return false;
                    }
                    tran.Commit();
                    return true;
                }
                catch
                {
                    msg = "发生错误";
                    tran.Rollback();
                    return false;
                }



        }



        #region 封装方法
        /// <summary>
        /// 获取出库单表
        /// </summary>
        /// <param name="OrderSn"></param>
        /// <returns></returns>
        private TShippingOrderDetail GetShippingOrderDetail(string OrderSn)
        {
            try
            {
                var shippingList = Context.TShippingOrderDetails.First(x => x.OrderSn == OrderSn);
                return shippingList;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 获取出库单详情表
        /// </summary>
        /// <param name="OrderSn"></param>
        /// <returns></returns>
        private TShippingLockCell GetShippingLockCell(string OrderSn)
        {
            try
            {
                var shippingLockcell = Context.TShippingLockCells.First(x => x.OrdeSn == OrderSn);
                return shippingLockcell;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 根据库房编码查询库房名称
        /// </summary>
        /// <param name="WarehouseCode"></param>
        /// <returns></returns>
        private string GetWarehoseName(string WarehouseCode)
        {
            try
            {
                var list = Context.TWarehouses.First(x => x.Code == WarehouseCode);
                return list.Name;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 查询库存表数据
        /// </summary>
        /// <param name="PalletBarcode"></param>
        /// <param name="WarehouseCode"></param>
        /// <returns></returns>
        private TInventory GetInventoryId(string PalletBarcode, string WarehouseCode)
        {
            try
            {
                var list = Context.TInventories.First(x => x.PalletBarcode == PalletBarcode && x.WarehouseCode == WarehouseCode);
                return list;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 获取入库垛信息
        /// </summary>
        /// <param name="PalletBarcode"></param>
        /// <returns></returns>
        private TStackingStorage GetStacking(string PalletBarcode)
        {
            try
            {
                var list = Context.TStackingStorages.First(x => x.PalletBarcode == PalletBarcode);
                return list;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 获取所有库房
        /// </summary>
        /// <returns></returns>
        public List<TWarehouse> GetAllWarehouse()
        {
            return Context.TWarehouses.ToList();
        }



        #endregion
    }
}
